var BestRoutes = {

	GET_BEST_ROUTES_URL : "/company/pages/bestroutes",

	Validate : function validateForm() {
		var validFlag = true;
		paramOne = $("#start :selected").val();
		paramTwo = $("#finish :selected").val();
		if ((paramOne <= 0) || (paramOne > 23)) {
			$('form hr').after(
					'<span class="error">' + $('#error1').html() + '</span>');
			validFlag = false;
		}
		if ((paramTwo <= 0) || (paramTwo > 23)) {
			$('form hr').after(
					'<span class="error">' + $('#error2').html() + '</span>');
			validFlag = false;
		}
		if (paramOne === paramTwo) {
			$('form hr').after(
					'<span class="error">' + $('#error3').html() + '</span>');
			validFlag = false;
		}
		return validFlag;
	},

	SendQuery : function send() {

		jQuery.ajax({
			type : "POST",
			async : true,
			cashe : false,
			url : BestRoutes.GET_BEST_ROUTES_URL,
			data : $('#contentbody form').serialize(),
			beforeSend : function() {
				$('#load').css({
					'display' : 'table-cell'
				});
			},
			complete : function() {
				$('#load').css({
					'display' : 'none'
				});
			},
			success : function(data) {
				var res = $('#result');
				$('#result').empty();
				var table = $('<table></table>');
				var thead = $('<thead ></thead>').css({
					'text-align' : 'center'
				});
				var row = $('<tr></tr>');
				var column = $('<th></th>').text($('#best').html());
				row.append(column);
				column = $('<th></th>').text($('#features').html());
				row.append(column);
				thead.append(row);
				table.append(thead);

				var count = 0;
				
				try {
					var reply = jQuery.parseJSON(data);
					
					$.each(reply, function(key, value) {

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#cities').html());
						row.append(column);
						column = $('<th></th>');
						var cities = value.citiesNames;
						for ( var i = 0; i < cities.length - 1; i++) {
							column.text(column.text() + cities[i] + ', ');
						}
						column.text(column.text() + cities[cities.length - 1]);
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#distance').html());
						row.append(column);
						column = $('<th></th>').text(
								value.lengthRoute + $('#km').html());
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#priority').html());
						row.append(column);
						column = $('<th></th>').text(value.roadPriority);
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#intensity').html());
						row.append(column);
						column = $('<th></th>').text(value.flowIntensity);
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#quality').html());
						row.append(column);
						column = $('<th></th>').text(value.roadQuality);
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#width').html());
						row.append(column);
						column = $('<th></th>').text(value.roadWidth);
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#speed').html());
						row.append(column);
						column = $('<th></th>').text(
								value.windSpeed + $('#mps').html());
						row.append(column);
						table.append(row);

						row = $('<tr></tr>');
						column = $('<th></th>').text($('#temperature').html());
						row.append(column);
						column = $('<th></th>').text(
								value.temperature + "\u00B0" + 'C');
						row.append(column);
						table.append(row);
						
						count++;
						
						if (count < 4) {						
							row = $('<tr></tr>');
							column = $('<th></th>').
							css({
								'text-align' : 'center'
							}).text($('#possible').html() + ' ' + count);
							row.append(column);
							column = $('<th></th>').
							css({
								'text-align' : 'center'
							}).text($('#features').html());
							row.append(column);
							table.append(row);						
						}
					});

					res.append(table);
					$('.hide').each(function() {
						$(this).css({
							'display' : 'block'
						});
					});	
				} catch(e) {
				    $('form hr').after(
							'<span class="error">' + data + '</span>');
				}									
			},
			error : function() {
			}
		});

	}
};

$(document).ready(function() {

	$("#send").click(function() {
		$('#contentbody').find('.error').remove();
		var flag = BestRoutes.Validate();
		if (flag) {
			BestRoutes.SendQuery();
		}
		return false;
	});

});
